//part1:C09:ex9.44.cpp
//replace a string.
#include <iostream>
#include <vector>
#include <string>
using namespace std;

void replace_string(string& str, const string& oldVal, const string& newVal) {
  int idx = 0;
  while ((idx = str.find(oldVal, idx)) != string::npos) {
    str.replace(idx, oldVal.size(), newVal);
    idx += newVal.size();
  }
}

int main(void) {
  string s1 = "tho thru tho!";
  replace_string(s1, "thru", "through");
  cout << s1 << endl;

  replace_string(s1, "tho", "through");
  cout << s1 << endl;
  
  replace_string(s1, "through", "");
  cout << s1 << endl;

  return 0;
}
